package com.boot.core.async.action;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public abstract class AbstractLogAction<R> {
    protected final String methodName;
    protected final Object[] args;

    public AbstractLogAction(String methodName, Object... args) {
        this.methodName = methodName;
        this.args = args;
    }

    protected void log(R result, Throwable throwable) {
        if (throwable != null) {
            logThrowable(throwable);
        } else {
            logResult(result);
        }
    }

    protected void logThrowable(Throwable throwable) {
        log.error("call {} fail, args:{} , ex:{}", methodName, args, throwable.toString(), throwable);
    }

    protected void logResult(R result) {
        if (isLogResult()) {
            log.info("call {} success, args:{}, result:{}", methodName, args, result);
        } else {
            log.info("call {} success, args:{}", methodName, args);
        }
    }

    protected boolean isLogResult() {
        //这里是动态配置开关，用于动态控制日志打印，开源动态配置中心可以使用nacos、apollo等，如果项目没有使用配置中心则可以删除
        return true;
    }

}
